package kiratest;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.Test;

import java.io.IOException;

/**
 * Created by wangning on 2016/6/29.
 */

/*
发送请求之前设置hearder 直接使用header()会覆盖原有的head 使用addhead()使head变成list
 */
public class accessing_headers {
    private final OkHttpClient client = new OkHttpClient();
    @Test
    public void run() throws Exception {
        Request request = new Request.Builder()
                .url("https://api.github.com/repos/square/okhttp/issues")
                .header("User-Agent", "OkHttp Headers.java")
                .addHeader("Accept", "application/json; q=0.5")
                .addHeader("Accept", "application/vnd.github.v3+json")
                .build();
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        System.out.println("Server: " + response.header("Server"));
        System.out.println("Date: " + response.header("Date"));
        System.out.println("Vary: " + response.headers("Vary"));
        System.out.println("accept:"+response.headers("Accept"));
        System.out.println("******************************************************************");
        System.out.println("list:"+response.headers());
    }


    @Test
    public void kiratest() throws Exception {
        Request request = new Request.Builder()
                .url("http://localhost:8080/restful/2/delete")
                .header("kiratest","Akira")
                .build();

        OkHttpClient client = new OkHttpClient();
        Response response = client.newCall(request).execute();
        if(!response.isSuccessful()) throw new IOException("code fail"+response);
        System.out.println("Vary=="+response.header("Vary"));
        System.out.println(response.headers());
        System.out.println(response.headers());
    }
}
